/* eslint-disable object-curly-newline,prefer-destructuring */
/*   toolbar的业务功能   */
import uuid from 'node-uuid';
import * as comFunc from '../../Common/ComFunc';
import * as utils from '../../../utils/utils';
import * as config from '../../../utils/config';
import * as commonBusiness from '../business/commonBusiness';
import * as salesOrderBusiness from '../business/sales/salesOrder/salesOrderBusiness';
import * as services from '../../../services/services'; /*   服务类   */

// ----------------------------新增start---------------------------- //
/**   点击新增按钮   */
export function handleAdd(props) {
  /*   待用数据赋值   */
  const { masterShowConfig, slaveShowConfig } = props;
  /*   主表数据   */
  const masterData = comFunc.getDefaultMasterData(masterShowConfig);
  /*   状态相关的都干掉   */
  masterData.bInvalid = false;
  masterData.bCheck = false;
  /*   主表showType渲染,从表showType不渲染,但是从表需要渲染   */
  props.isMasterRender = comFunc.setMasterRenderTrueOrFalse(masterShowConfig, true);
  props.editTableRender = true;
  props.isSlaveRender = comFunc.setSlaveRenderTrueOrFalse(slaveShowConfig, [], false);
  /*   返回值赋值   */
  props.masterData = masterData;
  props.slaveData = [];
  props.disabled = false;
  /*   返回值   */
  return props;
}
// ----------------------------新增end  ---------------------------- //

// ----------------------------修改start---------------------------- //
/**   点击修改按钮操作   */
export function handleEdit(props) {
  /*   待用数据赋值   */
  const { masterShowConfig, slaveShowConfig, slaveData } = props;
  /*   主从表showType渲染,从表也需要渲染   */
  props.isMasterRender = comFunc.setMasterRenderTrueOrFalse(masterShowConfig, true);
  props.editTableRender = true;
  props.isSlaveRender = comFunc.setSlaveRenderTrueOrFalse(slaveShowConfig, slaveData, true);
  /*   返回值赋值   */
  props.disabled = false;
  /*   返回值   */
  return props;
}
// ----------------------------修改end  ---------------------------- //

// ----------------------------删除start---------------------------- //
/*   toolbar删除   */
export function handleDel(props) {
  const { title, confirm } = props;
  const onOkDel = handleOkDel;
  confirm({
    title,
    onOk() {
      onOkDel(props);
    },
    onCancel() {
    },
  });
}

/* 确定要删除 */
export async function handleOkDel(props) {
  const { token, sModelsId } = props;
  /*   数据参数   */
  const value = handleReturnDelValues(props);
  const url = `${config.server_host}business/delMasSlaBusinessData?sModelsId=${sModelsId}`;
  /*   接收返回值   */
  const { data } = await services.postValueService(token, value, url);
  if (data.code === 1) {
    /*   数据删除成功   */
    handleDelDataSuccess(props);
  } else { /*   失败   */
    comFunc.handleCommonFail(props, data);
  }
}

/* 数据删除成功跳转到新路由即pane */
export function handleDelDataSuccess(props) {
  const { app } = props;
  let { panes, currentPane } = app;
  /*  数据删除成功后刷新跳转路由数据 */
  currentPane.refresh();
  /* 删除单据后退出当前路由后,标签panes变化后的集合 */
  panes = panes.filter(pane => pane.key !== currentPane.key);
  /* 跳转到panes集合的最后一个路由，因为panes集合一定含有主页路由所以panes的集合大于等于1 */
  currentPane = panes[panes.length - 1];
  props.onRemovePane(panes, currentPane);
}

/*   删除接口需要的参数   */
export function handleReturnDelValues(props) {
  const { sId, masterConfig, slaveConfig, checkConfig } = props;
  const value = {
    sClientType: '1',
    data: {
      sMasterTable: masterConfig.sTbName,
      sId: [sId],
      sSlaveTable: [
        {
          tableName: slaveConfig.sTbName,
          relationName: 'sParentId',
        }, {
          tableName: checkConfig.sTbName,
          relationName: 'sBillId',
        }],
    },
  };
  return value;
}
// ----------------------------删除end  ---------------------------- //

// ----------------------------保存start---------------------------- //
/**   toolbar保存验证   */
export function handleSaveValidate(props) {
  /*   待用数据声明   */
  const { slaveData, masterData, delData } = props;
  /*   获取待存储数据(主表,从表)   */
  const saveData = getSaveData(masterData, slaveData, delData);
  /*   返回值赋值   */
  props.masterSaveData = saveData.masterData;
  props.slaveSaveData = saveData.slaveData;
  /*   保存之前的验证   */
  props = commonBusiness.handleSaveValidate(props);
  /*   返回值   */
  return props;
}

/*   toolbar保存数据   */
export async function handleSaveData(props) {
  /*   主表数据处理   */
  props = handleSaveMaster(props);
  /*   从表数据处理   */
  props = handleSaveSlave(props);
  /*   数据参数   */
  const value = getSaveApiValue(props);
  /*   保存数据   */
  const json = await services.postValueService(
    props.app.token,
    value,
    `${config.server_host}business/addUpdateDelBusinessData?sModelsId=${props.sModelsId}`,
  );
  /*   接收返回值   */
  const { data } = json;
  /*   成功的话刷新页面(同步主表和从表数据),不成功就弹出错误提示   */
  if (data.code === 1) { /*   成功   */
    handleSaveSuccess(data, props);
  } else { /*   失败   */
    comFunc.handleCommonFail(props, data);
  }
  /*   返回值   */
  return props;
}

/**   保存成功   */
export function handleSaveSuccess(data, props) {
  /*   待用数据声明   */
  const {
    checkConfig,
    checkData,
    dataTotal,
    masterConfig,
    slaveConfig,
    masterShowConfig,
    slaveShowConfig,
    checkShowConfig,
    slaveColumn,
    checkColumn,
    sListFormmasterId,
    sFilterOrderBy,
    pages,
    bFilter,
    currentIndex,
    message,
    masterSaveData,
  } = props;
  /*   成功提示   */
  message.success(data.msg);
  /**   保存成功赋值   */
  const newData = handleSaveSuccessAssignment(data.dataset.rows, props);
  /*   调用高阶刷新   */
  props.onGetAllData({
    isRender: true,
    masterConfig,
    slaveConfig,
    checkConfig,
    masterData: newData.masterData,
    slaveData: newData.slaveData,
    checkData,
    dataTotal,
    sId: masterSaveData.sId,
    masterShowConfig,
    slaveShowConfig,
    checkShowConfig,
    slaveColumn,
    checkColumn,
    sListFormmasterId,
    sFilterOrderBy,
    pages,
    bFilter,
    currentIndex,
    isChangeHandleTypeUpdate: true,
  });
  /*   保存数据后刷新跳转路由数据   */
  handleCommonListRefresh(props);
}

/**   处理列表页刷新   */
export function handleCommonListRefresh(props) {
  /*   待用数据赋值   */
  const { app } = props;
  const { currentPane } = app;
  /*   传了刷新方法才刷新列表   */
  if (currentPane.refresh) {
    /*   更新当前页参数   */
    currentPane.isRender = undefined;
    currentPane.isRenderPart = false;
    currentPane.showRender = [];
    currentPane.showSlaveRender = [];
    currentPane.editTableRender = false;
    /*   刷新跳转路由数据   */
    currentPane.refresh();
  }
}

/**   保存成功赋值   */
export function handleSaveSuccessAssignment(data, masterSaveData) {
  return {
    masterData: handleSaveSuccessAssignmentMaster(data, masterSaveData),
    slaveData: handleSaveSuccessAssignmentSlave(data, masterSaveData),
  };
}

/**   保存成功赋值(主)   */
export function handleSaveSuccessAssignmentMaster(data, props) {
  /*   待用数据声明   */
  const { masterData, masterSaveData } = props;
  /*   主表的sId是一定要赋滴   */
  masterData.sId = masterSaveData.sId;
  /*   看看返回的数据是否是空   */
  if (utils.isNotEmptyArr(data)) {
    /*   真正的返回数据集   */
    const res = data[0];
    /*   遍历数据集   */
    for (const key of Object.keys(res)) {
      /*   _master结尾的数据为主表数据   */
      if (key.endsWith('_master')) {
        /*   取_master结尾的数据   */
        const masterRes = res[key];
        /*   遍历主表赋值数据并给主表赋值   */
        for (const resKey of Object.keys(masterRes)) {
          masterData[resKey] = masterRes[resKey];
        }
      }
    }
  }
  /*   返回值   */
  return masterData;
}

/**   保存成功赋值(从)   */
export function handleSaveSuccessAssignmentSlave(data, props) {
  /*   待用数据声明   */
  let { slaveData } = props;
  const { masterSaveData } = props;
  /*   这里其实就是给从表的parentId都赋上值,以后这里可能还要把返回的所有的数据都给附上值,现在暂时先不做,以后有了这种需求再说   */
  slaveData = handleSaveSlaveCommon(slaveData, masterSaveData);
  /*   返回值   */
  return slaveData;
}

/**   获取保存调用接口的value   */
export function getSaveApiValue(props) {
  /*   待用数据声明   */
  const { masterConfig, slaveConfig, masterSaveData, slaveSaveData } = props;
  /*   返回值   */
  return {
    data: [
      { sTable: masterConfig.sTbName, column: [{ ...masterSaveData }] },
      { sTable: slaveConfig.sTbName, column: [...slaveSaveData] },
    ],
    sClientType: '1',
  };
}

/**   保存主表数据   */
export function handleSaveMaster(props) {
  /*   待用数据声明   */
  const { sModelsId, masterSaveData } = props;
  /*   给主表数据赋值   */
  masterSaveData.sFormId = sModelsId;
  /*   返回值   */
  return props;
}

/**   保存从表数据   */
export function handleSaveSlave(props) {
  /*   待用数据赋值   */
  const { slaveData, masterSaveData, masterConfig } = props;
  const { sModelType } = masterConfig;
  /*   从表数据的通用处理   */
  props.slaveSaveData = handleSaveSlaveCommon(slaveData, masterSaveData);
  /*   销售订单   */
  if (sModelType === 'salesOrder') {
    props.slaveSaveData = salesOrderBusiness.handleSaveSlave(props.slaveSaveData);
  }
  /*   返回值   */
  return props;
}

/**   公共保存从表数据   */
export function handleSaveSlaveCommon(slaveData, masterSaveData) {
  /*   从表数据不为空就给从表数据赋值   */
  if (utils.isNotEmptyArr(slaveData)) {
    for (const each of slaveData) {
      each.sParentId = masterSaveData.sId;
    }
  }
  /*   返回值   */
  return [...slaveData];
}

/**   获取保存数据   */
export function getSaveData(masterData, slaveData, delData) {
  /*   返回值声明   */
  const obj = {};
  /*   获取主表数据   */
  masterData = getSaveMasterData(masterData);
  /*   获取从表数据   */
  slaveData = getSaveSlaveData(slaveData, delData);
  /*   返回值赋值   */
  obj.masterData = masterData;
  obj.slaveData = slaveData;
  /*   返回值   */
  return obj;
}

/**   获取保存主表数据   */
export function getSaveMasterData(masterData) {
  /*   返回值声明   */
  const masterSaveData = { ...masterData };
  /*   返回值赋值   */
  masterSaveData.maxBillNo = 'sBillNo';
  if (utils.isEmptyStr(masterSaveData.sId)) {
    masterSaveData.handleType = 'add';
    masterSaveData.sId = utils.createSid();
  } else {
    masterSaveData.handleType = 'update';
  }
  /*   返回值   */
  return masterSaveData;
}

/**   获取保存从表数据   */
export function getSaveSlaveData(slaveData, delData) {
  /*   返回值声明   */
  let saveDataRes = [];
  /*   从从表数据中过滤出发生change的数据   */
  const slaveSaveData = slaveData.filter(item => item.bIsChange);
  const delSaveData = delData.filter(item => item.handleType !== 'add');
  /*   从表change数据添加   */
  if (utils.isNotEmptyArr(slaveSaveData)) {
    saveDataRes = [...slaveSaveData];
  }
  /*   删除数据添加   */
  if (utils.isNotEmptyArr(delSaveData)) {
    saveDataRes = [...saveDataRes, ...delSaveData];
  }
  /*   返回值   */
  return saveDataRes;
}
// ----------------------------保存end  ---------------------------- //

// ----------------------------取消start---------------------------- //
/*   取消  */
export function handleCancel(props) {
  /*   待用数据声明   */
  const { dispatch, app } = props;
  const { currentPane } = app;
  /*   改变页签数据从高阶重新渲染   */
  dispatch({
    type: 'app/changePaneData',
    payload: {
      parentId: currentPane.key, /*   父页面编号   */
      isRender: true, /*   是否从高阶渲染   */
      isRenderPart: false, /*   是否进行部分渲染   */
    },
  });
}
// ----------------------------取消end  ---------------------------- //

// ----------------------------上一条、下一条、首条、末条start---------------------------- //
/*   上一条、下一条、首条、末条   */
export async function handleSearchData(props) {
  /*   待用数据声明   */
  const { sListFormmasterId, handleType, sFilterOrderBy, bFilter, app, formData, sModelsId, pages, pageSize, currentIndex, currentIndexFlag } = props;
  const { token } = app;
  const sIdArray = formData.filter(item => item.bGrd === false);
  const currentIndexPop = handleGetCurrentPosition(currentIndexFlag, currentIndex, pageSize, pages, handleType);
  const arrayMap = handleGetConfigureMap(formData);
  const { sId } = utils.isNotEmptyArr(sIdArray) ? sIdArray[0] : [{ sId: '' }];
  /*   数据参数   */
  const value = this.handleReturnValues('1', sFilterOrderBy, bFilter, sListFormmasterId, sId, currentIndexPop, arrayMap, handleType, pages.totalCount);
  const url = `${config.server_host}business/getBusinessDataByIndex?sModelsId=${sModelsId}`;
  /*   接收返回值   */
  const { data } = await services.postValueService(token, value, url);
  /*   成功的话刷新页面(同步主表和从表数据),不成功就弹出错误提示   */
  if (data.code === 1) {
    /*   数据查询成功   */
    props = handleSearchDataSuccess(data, props);
    return props;
  } else { /*   失败   */
    comFunc.handleCommonFail(props, data);
    return undefined;
  }
}

/*   获取数据在主表页面的位置序号   */
export function handleGetCurrentPosition(currentIndexFlag, currentIndex, pageSize, pages, handleType) {
  let currentIndexPop = currentIndexFlag ? currentIndex : ((pages.pageNum - 1) * pageSize) + Number.parseInt(currentIndex, 10) + 1;
  if (handleType === 'first') {
    currentIndexPop = 1;
  } else if (handleType === 'end') {
    currentIndexPop = pages.totalCount;
  }
  /* 返回值 */
  return currentIndexPop;
}

/*   获取详情页的模块主从表配置的sId   */
export function handleGetConfigureMap(formData) {
  const arrayMap = {}; /* 详情页的模块主从表配置的sId */
  for (const item of formData) {
    arrayMap[`${item.sId}`] = item.sId;
  }
  /* 返回值 */
  return arrayMap;
}

/* 返回接口参数  */
export function handleReturnValues(sClientType, sFilterOrderBy, bFilter, sListFormmasterId, sFormmasterId, currentPosition, configureMap, handleType, iCountSum) {
  const value = {
    sClientType,
    sFilterOrderBy,
    bFilter,
    sListFormmasterId,
    sFormmasterId,
    currentPosition,
    configureMap,
    handleType,
    iCountSum,
  };
  /* 返回值 */
  return value;
}

/*   数据查询成功   */
export function handleSearchDataSuccess(data, props) {
  const values = data.dataset.rows[0];
  const { currentPosition } = values;
  props = handleSearchDataToRefresh({
    ...props,
    values,
  });
  props.currentIndex = currentPosition;
  props.currentIndexFlag = true;
  props.editTableRender = true;
  return props;
}

/*  数据查询成功后数据刷新  */
export function handleSearchDataToRefresh(props) {
  /*   主表刷新   */
  props = handleMasterRefresh(props);
  /*   从表刷新   */
  props = handleSlaveRefresh(props);
  /*   审核刷新   */
  props = handleCheckRefresh(props);
  /*   返回值   */
  return props;
}

/*   数据查询主表刷新   */
export function handleMasterRefresh(props) {
  const { masterShowConfig, masterConfig, values } = props;
  props.masterData = values[masterConfig.sId][0];
  props.sId = props.masterData.sId;
  props.isMasterRender = comFunc.setMasterRenderTrueOrFalse(masterShowConfig, true);
  return props;
}

/*   数据查询从表刷新   */
export function handleSlaveRefresh(props) {
  const { slaveShowConfig, slaveConfig, values } = props;
  props.slaveData = values[slaveConfig.sId];
  /* 上一条、下一条、首条、末条从表刷新 */
  props = commonBusiness.handleSlaveData(props);
  props.editTableRender = true;
  props.isSlaveRender = comFunc.setSlaveRenderTrueOrFalse(slaveShowConfig, props.slaveData, true);
  return props;
}

/*   数据查询主表刷新   */
export function handleCheckRefresh(props) {
  const { checkShowConfig, checkConfig, checkData, values } = props;
  props.checkData = values[checkConfig.sId];
  props.isCheckRender = comFunc.setSlaveRenderTrueOrFalse(checkShowConfig, checkData, true);
  return props;
}
// ----------------------------上一条、下一条、首条、末条end  ---------------------------- //

// ----------------------------上查下查start---------------------------- //
export async function handleSearchUpDownThird(props) {
  /*   待用数据声明   */
  const { slaveData, masterData, sModelsId, selectedRowKeys, masterConfig, app, dispatch, upDownKey, message } = props;
  /*   验证   */
  if (handleSearchUpDownThirdVerify(selectedRowKeys, slaveData, upDownKey, message)) {
    return undefined;
  }
  /**   获取sqlCondition参数：{sSqlCondition,sId}   */
  const sqlConditionParameter = handleSqlConditionParameter(masterConfig, upDownKey);
  if (utils.isNotEmptyStr(sqlConditionParameter.sSqlCondition) && utils.isNotEmptyStr(sqlConditionParameter.sId)) {
    /*   获取三级菜单 待用数据声明   */
    const { token } = app;
    const sSlaveId = selectedRowKeys[0];
    const url = `${config.server_host}searchupdown/getSearchUpDown/${sqlConditionParameter.sId}?sModelsId=${sModelsId}`;
    const value = comFunc.getSqlCondition(sqlConditionParameter.sSqlCondition, sSlaveId, slaveData, masterData);
    /*   待用数据格式转换   */
    const fixValue = { sSqlCondition: value };
    /*   调用后台接口   */
    const { data } = await services.postValueService(token, fixValue, url);
    if (data.code === 1) {
      /*   数据查询成功   */
      props = handleSearchUpDownDataSuccess(props, data);
      return props;
    } else { /*   失败   */
      comFunc.handleCommonFail(data, dispatch);
      return undefined;
    }
  }
}

/*   校验上查下查三级菜单条件   */
export function handleSearchUpDownThirdVerify(selectedRowKeys, slaveData, key, message) {
  if (selectedRowKeys.length !== 1 && (key.indexOf('BtnUpCheck') > -1 || key.indexOf('BtnDownCheck') > -1)) {
    message.error('请选择一条数据');
    return true;
  }
  const selectedData = slaveData.filter(item => item.sId === selectedRowKeys[0]);
  if (key.indexOf('BtnUpCheck') > -1 && utils.isEmptyStr(selectedData[0].sOrderNo)) {
    message.error('该数据订单号为空');
    return true;
  }
}

/**   获取sqlCondition参数   */
export function handleSqlConditionParameter(masterConfig, key) {
  const obj = {
    sSqlCondition: '',
    sId: '',
  };
  /*    在主表配置中获取   */
  const gdsconfigformslaveEach = masterConfig.gdsconfigformslave.filter(item => item.sControlName === key);
  if (utils.isNotEmptyArr(gdsconfigformslaveEach)) {
    obj.sSqlCondition = gdsconfigformslaveEach[0].sSqlCondition;
    obj.sId = gdsconfigformslaveEach[0].sId;
  }
  return obj;
}
/*   上查下查三级菜单数据成功   */
export function handleSearchUpDownDataSuccess(props, data) {
  /*   待用数据声明   */
  const { upDownKey, masterShowConfig, slaveShowConfig, slaveData } = props;
  const values = data.dataset.rows;
  /*   key用于与二级菜单匹配   */
  props.toolBarSearchUpDownThirdMenuKey = upDownKey;
  props.toolBarSearchUpDownThirdMenuData = values;
  props.isMasterRender = comFunc.setMasterRenderTrueOrFalse(masterShowConfig, true);
  props.isSlaveRender = comFunc.setSlaveRenderTrueOrFalse(slaveShowConfig, slaveData, true);
  props.editTableRender = true;
  /*   返回值   */
  return props;
}

/*   单据页上查下查跳转页面   */
export async function handleSearchUpDown(props) {
  /*   待用数据声明   */
  const { dispatch, sTabId, upDownSid, upDownSActiveId } = props;
  dispatch({
    type: 'content/onRouter',
    payload: {
      url: `${config.server_host}gdsmodule/getGdsmoduleById/${upDownSActiveId}?sModelsId=${upDownSActiveId}`, /*   接口地址   */
      sParentId: sTabId, /*   父页面编号   */
      checkedId: upDownSid,
    },
  });
}
// ----------------------------上查下查end  ---------------------------- //

// ----------------------------审核消审start---------------------------- //
/**   toolbar审核(消审)   */
export function handleAudit(props) {
  /*   待用数据声明   */
  const { masterData, gdsformconst, message } = props;
  /*   作废订单不能审核   */
  if (masterData.bInvalid === true) {
    message.warning(gdsformconst.filter(item => (item.sName === 'sToVoid'))[0].showName);
    return undefined;
  }
  /**   数据审核   */
  props = dataAudit(props);
  /*   返回值   */
  return props;
}

/**   数据审核   */
export async function dataAudit(props) {
  /*   待用数据声明   */
  const { sModelsId, masterConfig, sId, app, auditFlag } = props;
  const { sModelType } = masterConfig;
  /*   数据参数   */
  const value = getAuditApiPar(auditFlag, sModelsId, sModelType, sId);
  /*   数据审核   */
  const json = await services.postValueService(
    app.token,
    value,
    `${config.server_host}business/doExamine?sModelsId=${sModelsId}`,
  );
  /*   接收返回值   */
  const { data } = json;
  /*   成功的话刷新页面(同步主表和从表数据),不成功就弹出错误提示   */
  if (data.code === 1) { /*   成功   */
    props = handleAuditSuccess(data, props);
    return props;
  } else { /*   失败   */
    comFunc.handleCommonFail(props, data);
    return undefined;
  }
}

/*   获取审核接口参数   */
export function getAuditApiPar(auditFlag, sModelsId, sModelsType, sId) {
  return {
    sClientType: '1',
    sProName: config.proData[sModelsType],
    paramsMap: {
      iFlag: auditFlag,
      iTmpCheck: 0,
      sFormGuid: sModelsId,
      sGuid: sId,
    },
  };
}

/**   审核成功   */
export function handleAuditSuccess(data, props) {
  /*   待用数据声明   */
  const { masterData, message, auditFlag, masterShowConfig, slaveShowConfig, slaveData } = props;
  /*   flag审核是1,消审是0   */
  masterData.bCheck = auditFlag === 1;
  /*   弹出提示   */
  message.success(data.msg);
  /*   返回值赋值   */
  props.masterData = masterData;
  /*   全都不渲染   */
  props.isMasterRender = comFunc.setMasterRenderTrueOrFalse(masterShowConfig, false);
  props.isSlaveRender = comFunc.setSlaveRenderTrueOrFalse(slaveShowConfig, slaveData, false);
  props.editTableRender = false;
  /*  审核(消审)后刷新跳转路由数据 */
  props.app.currentPane.refresh();
  /*   返回值   */
  return props;
}
// ----------------------------审核消审end  ---------------------------- //

// ----------------------------复制到start---------------------------- //
/*   单据页复制到   */
export function handleBillCopyTo(props) {
  /*   待用数据声明   */
  const { dispatch, masterData, slaveData, sTabId, masterAssign, slaveAssign, copyToSActiveId } = props;
  /*   根据主表赋值字段来获取数据   */
  const masterAssignData = comFunc.getCopyToMasterAssign(masterData, masterAssign);
  /*   根据从表赋值字段来获取数据   */
  const slaveAssignData = comFunc.getCopyToSlaveAssign(slaveData, slaveAssign, masterData);
  /*   复制到相应的页面   */
  dispatch({
    type: 'content/onRouter',
    payload: {
      url: `${config.server_host}gdsmodule/getGdsmoduleById/${copyToSActiveId}?sModelsId=${copyToSActiveId}`, /*   接口地址   */
      copyToData: {
        masterData: { ...masterAssignData }, /*  主表数据   */
        slaveData: [...slaveAssignData], /*   从表数据   */
      }, /*   复制到数据   */
      sParentId: sTabId, /*   父页面编号   */
    },
  });
}
// ----------------------------复制到end  ---------------------------- //

// ----------------------------复制从start---------------------------- //
/*   单据页复制从   */
export async function handleBillCopyFrom(props) {
  const { masterConfig, copyFromKey, copyFromSActiveId, app, slaveData, masterData } = props;
  /* 复制从具体按钮的配置 */
  const { gdsconfigformslave } = masterConfig;
  const copyFromConfig = gdsconfigformslave.filter(item => item.sControlName === copyFromKey)[0];
  /* 获取弹出框标题 */
  const copyFrom = {
    copyFromConfig,
  };
  const url = `${config.server_host}gdsmodule/getGdsmoduleById/${copyFromSActiveId}?sModelsId=${copyFromSActiveId}`; /*   接口地址   */
  const { data } = await services.getService(app.token, url);
  if (data.code === 1) {
    const { sMenuName } = data.dataset.rows[0];
    copyFrom.sMenuName = sMenuName;
  } else {
    comFunc.handleCommonFail(props, data);
  }
  /**   获取sqlCondition参数：{sSqlCondition,sId}   */
  const sqlConditionParameter = handleSqlConditionParameter(masterConfig, copyFromKey);
  if (utils.isNotEmptyStr(sqlConditionParameter.sSqlCondition) && utils.isNotEmptyStr(sqlConditionParameter.sId)) {
    const value = comFunc.getSqlCondition(sqlConditionParameter.sSqlCondition, null, slaveData, masterData);
    /*   待用数据格式转换   */
    const fixValue = { sSqlCondition: value };
    /*   获取弹窗数据   */
    const configUrl = `${config.server_host}business/getModelBysId/${copyFromSActiveId}?sModelsId=${copyFromSActiveId}`;
    const configReturn = (await services.getService(app.token, configUrl)).data;
    if (configReturn.code === 1) {
      const { formData } = configReturn.dataset.rows[0];
      const listsId = formData[0].sId;
      const listUrl = `${config.server_host}business/getBusinessDataByFormcustomId/${listsId}?sModelsId=${copyFromSActiveId}`;
      const listReturn = (await services.postValueService(app.token, fixValue, listUrl)).data;
      if (listReturn.code === 1) {
        const { dataSet } = listReturn.dataset.rows[0];
        for (const each of dataSet) {
          each.key = `${Date.now()}${uuid.v4()}`;
          Object.keys(each).forEach((child) => {
            if (child.startsWith('t')) {
              each[child] = comFunc.getDate(props, each[child]);
            }
          });
        }
        copyFrom.formData = formData;
        copyFrom.data = dataSet;
      } else {
        comFunc.handleCommonFail(props, listReturn);
      }
    } else {
      comFunc.handleCommonFail(props, configReturn);
    }
  }
  props.copyFrom = copyFrom;
  return props;
}
// ----------------------------复制从end---------------------------- //

// ----------------------------作废start---------------------------- //
/*   切换作废状态   */
export async function handleOkChangeInvaild(props) {
  /*   数据外置处理   */
  const { sId, sModelsId, masterData, masterConfig, invalidhandleType, app, message } = props;
  const value = {
    sClientType: '1',
    sId: [sId],
    sTableName: masterConfig.sTbName,
    handleType: invalidhandleType,
  };
  const url = `${config.server_host}checkModel/updatebInvalid?sModelsId=${sModelsId}`;
  const { data } = await services.postValueService(app.token, value, url);
  if (data.code === 1) {
    message.success(data.msg);
    masterData.bInvalid = invalidhandleType === 'toVoid'; /* 作废状态 */
    /*  切换作废状态后刷新跳转路由数据 */
    props.app.currentPane.refresh();
    /*   返回值   */
    return props;
  } else {
    message.error(data.msg);
    /*   返回值   */
    return undefined;
  }
}
// ----------------------------作废end  ---------------------------- //

// ----------------------------打印start---------------------------- //
/*   打印   */
export function handlePrint(props) {
  /*   待用数据声明   */
  const { app, sModelsId, formData, sId, printSid } = props;
  const { token } = app;
  const sMainArray = formData.filter(item => item.bGrd === false);
  const sMainTableId = utils.isNotEmptyArr(sMainArray) ? sMainArray[0].sId : ''; /* 主表配置的主sId */
  const queryFilterJson = handleGetFilterJson(formData, sId, sMainTableId);
  const urlPrint = `${config.server_host}printReport/printPdf/${printSid}?queryFilter=${queryFilterJson}&sModelsId=${sModelsId}&sMaintableId=${sMainTableId}&token=${encodeURIComponent(token)}`;
  window.open(urlPrint);
}

/*   窗体的查询条件   */
export function handleGetFilterJson(formData, sId, sMainTableId) {
  const queryFilter = {};
  for (const item of formData) {
    const sDataId = item.sId;
    queryFilter[sDataId] = sDataId === sMainTableId ? { sId } : {
      sSqlCondition: {
        sParentId: sId,
      },
    };
  }
  const queryFilterJson = escape(JSON.stringify(queryFilter));
  /* 返回值 */
  return queryFilterJson;
}

// ---------------------------- 打印end  ---------------------------- //
